package com.vm.fragment;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.vm.commons.VMConst;
import com.vm.model.ActiveSoft;
import com.vm.model.Soft;
import com.vm.myview.ViewUtils;
import com.vm.utils.JsonUtils;
import com.wwt.app.R;

public class NecessaryGameFragment extends BaseFragment {

	protected static final String TAG = "NecessaryAppActivity";
	private RequestQueue mQueue;
	private List<Soft> listsofts;
	ImageLoader mImageLoader;
	ImageListener listener;
	ViewUtils viewUtils;

	private View view;

	private boolean isPrepared;
	/** 是否已被加载过一次，第二次就不再去请求数据了 */
	private boolean mHasLoadedOnce;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		Log.i("NecessaryAppFragment", "onCreateView");

		if (view == null) {
			view = inflater.inflate(R.layout.necessary_game, null);
		} else {
			// 缓存的rootView需要判断是否已经被加过parent
			// 如果有parent需要从parent删除
			ViewGroup parent = (ViewGroup) view.getParent();
			if (parent != null) {
				parent.removeView(view);
			}
		}
		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		mQueue = Volley.newRequestQueue(getActivity());
		viewUtils = new ViewUtils(getActivity());

		isPrepared = true;
		lazyLoad();
	}

	@Override
	protected void lazyLoad() {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		if (!isPrepared || !isVisible || mHasLoadedOnce) {
			return;
		}

		new AsyncTask<Void, Void, Boolean>() {

			@Override
			protected void onPreExecute() {
				super.onPreExecute();
				// 显示加载进度对话框
				UIHelper.showDialogForLoading(getActivity(), "正在加载...", true);
			}

			@Override
			protected Boolean doInBackground(Void... params) {
				try {
					Thread.sleep(300);
					// 在这里添加调用接口获取数据的代码
					initData();
					// doSomething()
				} catch (Exception e) {
					e.printStackTrace();
				}
				return true;
			}

			@Override
			protected void onPostExecute(Boolean isSuccess) {
				if (isSuccess) {
					// 加载成功

					mHasLoadedOnce = true;
				} else {
					// 加载失败
				}
				// 关闭对话框
				UIHelper.hideDialogForLoading();
			}
		}.execute();
	}

	public void initData() {
		listsofts = new ArrayList<Soft>();
		String url = VMConst.REQUEST_URL + "getIndGame";
		JsonArrayRequest jsonObjectRequest = new JsonArrayRequest(url,
				new Listener<JSONArray>() {
					@Override
					public void onResponse(JSONArray arg0) {
						// arg0.length();

						for (int i = 0; i < arg0.length(); i++) {
							try {
								JSONObject jsonObject = arg0.getJSONObject(i);
								ActiveSoft activeSoft = JsonUtils.jsonToBean(
										jsonObject, ActiveSoft.class);
								LinearLayout found_ll_1 = (LinearLayout) view
										.findViewById(R.id.found_ll_1);

								found_ll_1.addView(viewUtils.dyAddView(i,
										activeSoft, 1));

							} catch (JSONException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}

						// SearchAdapter myAdapter = new SearchAdapter(
						// NecessaryAppActivity.this, listsofts);
						// ListView listView = (ListView)
						// findViewById(R.id.lv_necessary_app);
						// listView.setOnItemClickListener(lv_listener);
						// listView.setAdapter(myAdapter);
						//
						// Log.i(TAG, Integer.toString(listsofts.size()));
						// // arg0.getJSONObject(index);

					}

				}, new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError error) {
						Log.e(TAG, error.getMessage(), error);
					}
				});

		mQueue.add(jsonObjectRequest);
	}

}
